package mysql

import (
	"box-fresh-serv/box_fresh_servrpc/box_fresh_servrpc"
	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	Title string  `gorm:"type:varchar(50);not null;comment:'商品标题'"`
	Price float64 `gorm:"type:decimal(10,2);not null;comment:'商品金额'"`
	Stock int64   `gorm:"type:int(11);not null;comment:'商品库存'"`
	Image string  `gorm:"type:varchar(500);not null;comment:'商品图片'"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func (g *Goods) GoodsFindById(db *gorm.DB, id int64) interface{} {
	return db.Debug().Where("id=?", id).First(&g).Error
}

func (g *Goods) GoodsLists(db *gorm.DB, lists []*box_fresh_servrpc.GoodsList, in *box_fresh_servrpc.GoodsListRequest) ([]*box_fresh_servrpc.GoodsList, error) {

	err2 := db.Debug().Model(&g).Select("goods.id as goods_id",
		"goods.title",
		"goods.price",
		"goods.stock",
		"goods.image").Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&lists).Error
	return lists, err2
}
